<?php
use lib\controller\Controller;
use lib\factory\Factory;
use lib\factory\FactoryModel;
use lib\dao\query\Query;
use lib\dao\query\QueryAnd;
use lib\patterns\ServiceLocator;

class PinController extends Controller{
	
	public function telefono(){
		try{
			if($this->getParam('do_submit')){
				$this->View->layout = 'frontend';
				$tel = $this->getParam('telefono');
				$cant = $this->getParam('cantidad');
				$ticket = $this->getParam('ticket_id');

				$this->Model->telefono = $tel;
				$this->Model->ticket_id = $ticket;
				$this->Model->cantidad = $cant;
				$this->Model->token    = $this->_helper->Token(50);

				//esto podria estar dentro del helper token
				$this->Model->pin	   = mt_rand(0000, 9999);
				$this->Model->create();
				$this->_redirect("/pin/solicitar/token/".$this->Model->token, array('exit'=>true));
			}
		}catch (\Exception $e){
			$this->View->assign('msg', array('error',$e->getMessage()));
		}
		$this->View->display();
	}
	
	public function solicitar(){
		try{
			$token = $this->getParam('token');
			$this->View->layout = 'frontend';

			if($this->getParam('do_submit')){
				$this->Model->token = $token;

				//levantamos el pin mediente el token que llega por parametro
				$Q= new Query($this->Model);
				$Q->add(new QueryAnd('token',$token));
				if($this->Model->load($Q)){
					$pin = $this->getParam('pin');
					//verifico si esta activo
					if(!$this->Model->activo){
						$this->View->assign('msg', array('warning','Ya lo utilizaste'));
					}elseif($this->Model->pin == $pin){
						$this->Model->activo = false;
						$this->Model->update();
						$this->View->assign('msg', array('success','PIN OK'));
					//Cargamos el Ticket
						$FTicket=Factory::create(FactoryModel::getInstance(), 'Ticket');
						$Q = new Query($FTicket);
						$Q->add(new QueryAnd('id',$this->Model->ticket_id));
						if(!$ticket=($FTicket->load($Q))){
							 throw new \Exception('No existe sectores para esta compra');
						}
					//Me conecto al RPC
						$Config  = ServiceLocator::getConfig();
						$client = new Zend_XmlRpc_Client($Config->rpc->url);
						$datosCliente=$client->call('rpc.datos',$this->Model->telefono);
						if(empty($datosCliente)){
							$this->View->assign('msg', array('error','El cliente no existe'));
						}
						$resta=((int)$datosCliente['saldo']-((int)$FTicket->precio)*(int)$this->Model->cantidad);

						if(((int)$resta)<0){
							$this->View->assign('msg', array('error','No cuenta con saldo para realizar la compra'));
						}else{
							//Cargamos la compra.
							$Compra=Factory::create(FactoryModel::getInstance(), 'Compra');
							$Compra->ticket_id= $FTicket->id;
							$Compra->telefono = $datosCliente['telefono'];
							$Compra->nombre = $datosCliente['nombre'];
							$Compra->email  = $datosCliente['email'];
							$Compra->documento= $datosCliente['documento'];
							$Compra->token_c= $this->_helper->Token(20);
							$Compra->cantidad= $this->Model->cantidad;
							$Compra->create();
						    header("location: /compra/?token=$Compra->token_c");							
						}
					}
					else{
						$this->View->assign('msg', array('error','PIN incorrecto'));
					}
				}else{
					$this->View->assign('msg', array('error','PIN incorrecto 2'));
				}			
			}
			$this->View->assign('token', $token);
		}catch (\Exception $e){
			$this->View->assign('msg', array('error',$e->getMessage()));
		}
		$this->View->display();
	
	}

}